#!/usr/bin/env bash
################################################################################
# Test objective: check if the switch learning algorithm is working.
# Operations:
# 1) connect 3 ephemeral VALE ports (v0, v1, v2) to the same VALE switch.
# 2) send from v2 specifying a source MAC address and check that v0 and v1
#    receive the frame.
# 3) send from v0 using the previous MAC address as the destination MAC address
#    and check that v2 receives the frame while v1 does not.
################################################################################
source test_lib

parse_send_recv_arguments "$@"
verbosity="${verbosity:-}"
fill="${fill:-d}"
len="${len:-150}"

s_MAC=$(get_random_MAC)
d_MAC="FF:FF:FF:FF:FF:FF"

# Pre-opening interface that will be needed. This is needed to avoid a race
# condition between the sending and receiving ports.
functional $verbosity -i vale0:v0
check_success $? "pre-open vale0:v0"
functional $verbosity -i vale0:v1
check_success $? "pre-open vale0:v1"
functional $verbosity -i vale0:v2
check_success $? "pre-open vale0:v2"

# First send, every port should receive the frame.
functional $verbosity -i vale0:v0 -r "${len}:${fill}" -s "$s_MAC" -d "$d_MAC" &
p1=$!
functional $verbosity -i vale0:v1 -r "${len}:${fill}" -s "$s_MAC" -d "$d_MAC" &
p2=$!
functional $verbosity -i vale0:v2 -t "${len}:${fill}" -s "$s_MAC" -d "$d_MAC"
e3=$?
wait $p1
e1=$?
wait $p2
e2=$?
check_success $e1 "receive vale0:v0"
check_success $e2 "receive vale0:v1"
check_success $e3 "send vale0:v2"

# Second send, only v2 should receive the frame.
functional $verbosity -i vale0:v2 -r "${len}:${fill}" -d "$s_MAC"    &
p4=$!
functional $verbosity -i vale0:v1 -r "${len}:${fill}" -d "$s_MAC" -n &
p5=$!
functional $verbosity -i vale0:v0 -t "${len}:${fill}" -d "$s_MAC"
e6=$?
wait $p4
e4=$?
wait $p5
e5=$?
check_success $e1 "receive vale0:v0"
check_success $e2 "receive vale0:v1"
check_success $e3 "send vale0:v2"
